package controller;

/*
Cours :             LOG120 
Session :           Saison (hiver) 
Groupe :            01 
Projet :            Laboratoire #2
Etudiant(e)(s) :    Jeremy Moreau
                    Alexandre Richard
                    Philippe Pigeon
Code(s) perm. :     MORJ91010106
					RICA10028806 
                    PIGP08058903
Professeur :        Ghizlane El Boussaidi  
Date remise :       2012-03-010  
*/

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import view.*;
import db.DataClass;
import model.*;
import java.util.Random;

public class CommandeController extends AbstractController
{
	RestaurantView rView;
	MenuView mView;
	CommandeView cView;
	CommandFinalView cfView;
	
	Commande commande;
	MenuModele menu;
	
	JFrame previewFrame;
	JFrame mainFrame;
	
	public CommandeController()
	{
		commande = new Commande();
		menu = new MenuModele();
		
		rView = new RestaurantView(this, DataClass.getRestaurants());
		mView = new MenuView(this);
		cView = new CommandeView(this);
		cfView = new CommandFinalView(this);
		
		menu.addObserver(mView);
		commande.addObserver(cView);
		commande.addObserver(cfView);
		
		initMainFrame();
		initPreviewFrame();
	}
	
	private void initMainFrame()
	{
		//frame
		mainFrame = new JFrame("Placer une commande");
				
		//menu
		JMenuBar menuBar = new JMenuBar();
		mainFrame.setJMenuBar(menuBar);
		
		JMenu mnFichier = new JMenu("Fichier");
		menuBar.add(mnFichier);
		
		JMenuItem mntmLogout = new JMenuItem("Logout");
		mntmLogout.setAction(new LogoutAction(mainFrame));
		mnFichier.add(mntmLogout);
		
		JMenuItem mntmQuitter = new JMenuItem("Quitter");
		mntmQuitter.setAction(new QuitterAction());
		mnFichier.add(mntmQuitter);
		
		//panels
		mainFrame.getContentPane().add(rView);
		mainFrame.getContentPane().add(mView);
		mainFrame.getContentPane().add(cView);
		
		//options
		mainFrame.setResizable(false);
		mainFrame.setBounds(100, 100, 458, 319);
		mainFrame.setLayout(null);
		mainFrame.setVisible(true);
	}
	
	private void initPreviewFrame()
	{
		//preview frame
		previewFrame = new JFrame("Confirmer une commande");
		
		previewFrame.getContentPane().add(cfView);
		
		previewFrame.setBounds(100, 100, 450, 300);
		previewFrame.setLayout(null);
		previewFrame.setVisible(false);
	}
	
	public void updateSelectedResto(Restaurant _r)
	{
		//commande.clear();//1-resto -> 1-commande
		menu.setPlats(_r.getFirstMenu().getPlats());
	}
	
	public void ajouterLigneCommande(Plat _p, int _qty)
	{
		commande.addLigneCommande(rView.getSelectedRestaurant(), new LigneCommande(_p, _qty));
	}
	
	public void retirerLigneCommande(LigneCommande ligneCommande)
	{
		commande.removeLigneCommande(ligneCommande);
	}
	
	public void previewCommande()
	{
		previewFrame.setVisible(true);
        commande.setIdConfirmation(new Random().nextInt(100));
	}

	
	public void cancelPreview() 
	{
		previewFrame.setVisible(false);		
	}
	
	public void submitCommand(int _heure, int _minute, String _adresse, String _ville) 
	{
		String msg = "";
		
		msg += "Nouvelle entree dans le carnet de commandes: \n";
		msg += "Liv# " + commande.getIdConfirmation() + "\n";
		msg += "Prix: " + String.format("%.2f $", (commande.getTotal()*1.075)*1.05) + "\n";
		msg += "Livraison pour " + getCurrentClient().getPrenom() + " " + getCurrentClient().getNom() + "\n";
		msg += "Attendue pour " + _heure + "h"+ _minute + "\n";
		msg += "Location: \n";
		msg += "     " + _adresse + "\n";
		msg += "     " + _ville + "\n";
		
		System.out.println(msg);
		JOptionPane.showMessageDialog(null, msg);
		
		commande.setClient(getCurrentClient());
		commande.setAdresse(_adresse + " " + _ville);
		commande.setDeliveryTime(_heure, _minute);
		commande.setReady(true);
		DataClass.getCarnetDeCommande().add(commande);
		
		commande = new Commande();
		commande.addObserver(cView);
		commande.addObserver(cfView);
		
		cView.update(commande, null);
		
		previewFrame.setVisible(false);		
	}
	
	public Client getCurrentClient()
	{
		return DataClass.getCurrentClient();
	}	
}
